package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class LMS extends Activity implements OnClickListener {

	Button AllCourse;
	Button MyCourse;
	Button MyProfile;
	
	String userLink; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lms);

		AllCourse = (Button) findViewById(R.id.btnLmsAllCourses);
		MyCourse = (Button) findViewById(R.id.btnLmsMyCourses);
		MyProfile = (Button) findViewById(R.id.btnLmsMyProfile);

		AllCourse.setOnClickListener(this);
		MyCourse.setOnClickListener(this);
		MyProfile.setOnClickListener(this);
		
		Intent intename = getIntent();
		String testValue = intename.getStringExtra("logout");
		userLink = intename.getStringExtra("link");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.lm, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == AllCourse) {
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			String url_lib_auth = "http://lms.apiit.lk/course/index.php?categoryid=5";
			request.setUrl(url_lib_auth);

			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("test","/html[1]/body[1]/div[2]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/label[1]---");
			//xpathMap3.put("link","/html[1]/body[1]/div[2]/div[2]/div[1]/div[1]/a[1]---href");
			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();

			request.setParametersMap(parametersMap);
			request.setType("GET");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String testResult = respones.getResultsMap().get("test");
			
			Toast.makeText(getApplicationContext(), testResult,
					Toast.LENGTH_LONG).show();

			Intent i = new Intent(this, AllCourses.class);
			startActivity(i);
		}
		if (v == MyCourse) {
			
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			String url_lib_auth = "http://lms.apiit.lk/my/";
			request.setUrl(url_lib_auth);

			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("course1","/html[1]/body[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/h2[1]/a[1]---");
			xpathMap.put("course2","/html[1]/body[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[2]/div[1]/h2[1]/a[1]---");
			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();

			request.setParametersMap(parametersMap);
			request.setType("GET");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String result1 = respones.getResultsMap().get("course1");
			String result2 = respones.getResultsMap().get("course2");
			
			Toast.makeText(getApplicationContext(), result1 + result2,
					Toast.LENGTH_LONG).show();

			Intent i = new Intent(this, MyCourses.class);
			i.putExtra("course1", result1);
			i.putExtra("course2", result2);
			startActivity(i);
		}
		if (v == MyProfile) {
			
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl(userLink);
			Map<String, String> xpathMap = new HashMap<String, String>(); ///html[1]/body[1]/div[2]/div[2]/div[1]/div[1]/a[1]
			xpathMap.put("name","/html[1]/body[1]/div[2]/div[2]/div[1]/div[1]/a[1]---");
			xpathMap.put("country","/html[1]/body[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/dl[1]/dd[1]---");
			xpathMap.put("city","/html[1]/body[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/dl[1]/dd[2]---");
			xpathMap.put("course1","/html[1]/body[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/dl[1]/dd[3]/a[1]---");
			xpathMap.put("course2","/html[1]/body[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/dl[1]/dd[3]/a[2]---");
			xpathMap.put("firstAccess","/html[1]/body[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/dl[1]/dd[4]---");
			xpathMap.put("lastAccess","/html[1]/body[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/dl[1]/dd[5]---");
			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			

			String name =respones.getResultsMap().get("name").trim();
			String country =respones.getResultsMap().get("country").trim();
			String city =respones.getResultsMap().get("city").trim();
			String course1 =respones.getResultsMap().get("course1").trim();
			String course2 =respones.getResultsMap().get("course2").trim();
			String firstAccess =respones.getResultsMap().get("firstAccess").trim();
			String lastAccess =respones.getResultsMap().get("lastAccess").trim();
			
			Toast.makeText(getApplicationContext(), firstAccess,
					Toast.LENGTH_LONG).show();
			
			Intent i = new Intent(this, MyProfile.class);
			i.putExtra("name", name);
			i.putExtra("country", country);
			i.putExtra("city", city);
			i.putExtra("course1", course1);
			i.putExtra("course2", course2);
			i.putExtra("firstAccess", firstAccess);
			i.putExtra("lastAccess", lastAccess);
			startActivity(i);
		}
	}

}
